package com.oolatina.fragments;

import java.util.List;

import newspackage.APIParser;
import newspackage.AgendaBean;
import newspackage.WebServices;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.oolatina.R;
import com.oolatina.activity.OOLatinaApplication;
import com.oolatina.adapters.AgendaAdapter;

public class AgendaFragment extends BaseFragment {
	ListView listView;
	OOLatinaApplication oolatina;
	private ProgressBar spinner;
	private String agendaJson;
	private int elementNum = 10;
	private AgendaFragment agendaFrag = this;
	private AgendaAdapter adapter2;
	private RelativeLayout agendaViewWrap;
	private TextView noItemFound;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_agenda_view, null);
		spinner = (ProgressBar)mView.findViewById(R.id.progressBar1);
	    spinner.setVisibility(View.GONE);
	    agendaViewWrap = (RelativeLayout)mView.findViewById(R.id.agendaViewWrap);
		listView = (ListView) mView.findViewById(R.id.agendaList);
		noItemFound = (TextView) mView.findViewById(R.id.noItemFound);
		checkWebServices("", "", "", "", "");
		return mView;
	}

	public void checkWebServices(String kind, String music, String recherche, String calendar, String city) {
		showLoading();
		noItemFound.setVisibility(View.GONE);
		listView.setAdapter(null);
		WebServices.getAgendas(kind, music, recherche, calendar, city, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String arg0) {
				agendaJson = arg0;
//				String json = getAgendaJson(elementNum);
				List<AgendaBean> eventList = APIParser.getAgendas(agendaJson);
				if (eventList.size() == 0) {
					noItemFound.setVisibility(View.VISIBLE);
				}
//				if (eventList.size() > 0) {
				adapter2 = new AgendaAdapter(getActivity(),
							eventList, getActivity(), agendaFrag);
				
				listView.setAdapter(adapter2);
				stopLoading();

//				}

			}

		});
	}

	public void showLoading() {
		spinner.setVisibility(View.VISIBLE);
	}
	
	public void stopLoading() {
		spinner.setVisibility(View.GONE);
	}
//	public void addMoreItems() {
//		spinner.setVisibility(View.VISIBLE);
//		String json = getAgendaJson(elementNum);
//		List<AgendaBean> eventList = APIParser.getAgendas(json);
//		if (eventList.size() > 0) {
//			adapter2.addItems(eventList);
//			spinner.setVisibility(View.GONE);
//		}
//	}
	
	private String getAgendaJson(int elementNum) {
		if (agendaJson == null || "".equals(agendaJson)) {
			return "";
		}

		String str = agendaJson;
		String findStr = "{\"event\":{\"day\":\"";
		int lastIndex = 0;
		int count = 0;
		

		while(lastIndex != -1){
			if (count  == elementNum + 1) {
				break;
			}
		       lastIndex = str.indexOf(findStr,lastIndex);

		       if( lastIndex != -1){
		             count ++;
		             lastIndex+=findStr.length();
		      }
		}
		if (lastIndex != -1) {
			lastIndex -= (findStr.length() + 1);
		} else {
			lastIndex = agendaJson.length() - 1;
		}
		String sub =  agendaJson.substring(0, lastIndex);
		agendaJson = "[" + agendaJson.substring(lastIndex + 1, agendaJson.length());
		
		return sub + "]";
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
